package tiled.simple.gwt.client;

import java.util.HashMap;

import tiled.simple.core.Map;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.RootPanel;

public class SimpleTMX_GWT_Platformer implements EntryPoint {
	   static final int refreshRate = 15;
	    HTMLPanel panel;
	    static final String id = "gamePanel";

	    Controls controls;
	    Game game;

	    FocusPanel focusPanel;
	    
	    private final GameServiceAsync gameService = (GameServiceAsync) GWT.create(GameService.class);

		public void onModuleLoad() {
	    	gameService.getGameData(new AsyncCallback<HashMap<String,Map>>() {
	            public void onFailure(Throwable caught) {
	            	Window.alert(caught.getMessage());
	            }

	            public void onSuccess(HashMap<String,Map> maps) {
	            	controls = new Controls();
	            
	            	game = new Game(maps,controls);

	        	    panel = new HTMLPanel("<div class=\"content\" id=\"" + id + "\"></div>");
	                panel.setStyleName("contentHolder");
	                RootPanel.get().setStyleName("contentHolder");
	                
	                Window.addResizeHandler(new ResizeHandler() {
	                	 
	                    public void onResize(ResizeEvent event)
	                    {
	                    	resize(panel.getOffsetWidth(),panel.getOffsetHeight());
	                    }
	                });

	                focusPanel = new FocusPanel(panel);
	            	focusPanel.addKeyDownHandler(controls);
	            	focusPanel.addKeyUpHandler(controls);
	            	RootPanel.get().add(focusPanel,0,0);
	            	focusPanel.setSize("100%", "100%");
	            	resize(panel.getOffsetWidth(),panel.getOffsetHeight());
	            	
	            	
	            	
	                
	            	previousTimeInMills=System.currentTimeMillis();
	            	
	                final Timer timer = new Timer() {
	                    public void run() {
	                       drawSomethingNew();
	                    }
	                };
	                timer.scheduleRepeating(refreshRate);
	            }
	        });

		}
		
		private int frameNumber=0;
		private long previousTimeInMills=0;
		
		private void drawSomethingNew() {
			//focusPanel.setFocus(true);
			focusPanel.setFocus(true);
			panel.clear();
		
			frameNumber++;
			
			long currentTimeInMills=System.currentTimeMillis();
			long delay=currentTimeInMills-previousTimeInMills;
			
			if(frameNumber%2==1){
				game.render(canvas1.getContext2d(),(int)delay);
				panel.add(canvas1, id);
			}else{
				game.render(canvas2.getContext2d(),(int)delay);
				panel.add(canvas2, id);
			}
			
			previousTimeInMills=currentTimeInMills;
		}

		private Canvas canvas1=null;
		private Canvas canvas2=null;
		
		private void resize(int width, int height)
	    {

			width = Math.max(0, width-2);
	        height = Math.max(0,height-2);
 
	        panel.clear();

			canvas1=Canvas.createIfSupported();
			canvas1.setStyleName("mainCanvas");
			canvas2=Canvas.createIfSupported();
			canvas2.setStyleName("mainCanvas");

	        resizeCanvas(canvas1,width,height);
	        resizeCanvas(canvas2,width,height);
	        
	        game.resize(width,height);
	    }
		
		private void resizeCanvas(Canvas canvas,int width, int height) {
	        canvas.setWidth(width + "px");
	        canvas.setCoordinateSpaceWidth(width);
	        canvas.setHeight(height + "px");
	        canvas.setCoordinateSpaceHeight(height);
	        canvas.setPixelSize(width, height);
		}
}
